package flymoke.swing.jtable;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;

public class BooleanCellEditor extends DefaultCellEditor {

  public BooleanCellEditor() {
    super(new JCheckBox());
    JCheckBox checkBox = (JCheckBox) getComponent();
    checkBox.setHorizontalAlignment(JCheckBox.CENTER);
  }

  public Object getCellEditorValue() {
    return ((JCheckBox) getComponent()).isSelected() ? "1" : "0";
  }

  public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {

    JCheckBox c = (JCheckBox) super.getTableCellEditorComponent(table, value,
      isSelected, row, column);

    c
      .setSelected(value != null
        && (value.equals(Boolean.TRUE) || value.equals("1")));

    return c;
  }

}